package com.example.videoproject.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.videoproject.pojo.Unite;
import com.example.videoproject.pojo.Video;
import com.example.videoproject.pojo.Videohistory;
import com.example.videoproject.pojo.Videolike;
import com.example.videoproject.service.IPersonService;
import com.example.videoproject.service.IVideoService;
import com.example.videoproject.service.IVideohistoryService;
import com.example.videoproject.service.IVideolikeService;
import com.example.videoproject.utils.Code;
import com.example.videoproject.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Stream;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 二组
 * @since 2022-07-23
 */
@RestController
@RequestMapping("/history")
public class VideohistoryController {

    @Autowired
    private IVideohistoryService historyService;

    @Autowired
    private IVideoService videoService;

    //添加历史记录
    @PostMapping("add")
    public Result add(Integer vid,Integer uid){
        if (uid!=null){
            String strDateFormat = "yyyy/MM/dd HH:mm:ss";
            SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
            Videohistory videohistory=new Videohistory(uid,vid,null,sdf.format(new Date()),0);
            historyService.save(videohistory);
            //播放量加一
            Video video = videoService.getById(vid);
            Integer vcount = video.getVcount();
            ++vcount;
            video.setVcount(vcount);
            videoService.updateById(video);


            return new Result(Code.INSERT_OK,null);
        }
        return new Result();
    }

    //删除历史记录
    @PostMapping("delete")
    public Result delete(Integer hid){
        boolean b = historyService.removeById(hid);
        return new Result(Code.DELETE_OK,null);
    }

    //清空历史记录
    @PostMapping("deleteAll")
    public Result deleteAll(Integer uid){
        QueryWrapper<Videohistory> wrapper=new QueryWrapper<>();
        wrapper.eq("uid",uid);
        historyService.remove(wrapper);
        return new Result(Code.DELETE_OK,null);
    }

    //搜索历史记录
    @PostMapping("find")
    public Result find(Integer uid,String search){
        List<Unite> list = historyService.select(uid);
        //按时间排序
        Stream<Unite> sorted = list.stream().sorted(Comparator.comparing(Unite::getVplaytime).reversed());

        List<Unite> uniteList=new ArrayList<>();

        if (search.trim().length()!=0){
            list.forEach(temp->{
                if (temp.getVtitle().contains(search)){
                    uniteList.add(temp);
                }
            });
            Stream<Unite> uniteStream = uniteList.stream().sorted(Comparator.comparing(Unite::getVplaytime).reversed());
            return new Result(Code.SELECT_OK,uniteStream);
        }else {
            return new Result(Code.SELECT_OK,sorted);
        }
    }

}

